import axios from 'axios';
import request from '@/utils/request';
import { Pagination } from '@/types/global';
import { UserState } from '@/store/modules/user/types';

// 登录参数
export interface LoginData {
  username: string;
  password: string;
  loginType: number;
}

// 登录返回
export interface LoginRes {
  token?: string;
}

// 资源信息
export interface Meta {
  icon: string;
  title: string;
  link: string;
  noCache: boolean | null;
}

// 资源信息
export interface ServerMenu {
  name: string;
  path: string;
  query: string;
  component: string;
  redirect: string | null;
  hidden: boolean;
  alwaysShow: boolean | null;
  meta: Meta;
  children: ServerMenu[];
}

// 用户数据
export interface UserData {
  id?: number;
  username?: string;
  password?: string;
  name?: string;
  nickName?: string;
  tel?: string;
  email?: string;
  gender?: number;
  status?: number;
  deptIds?: [];
  postIds?: [];
  roleIds?: [];
}

// 用户密码数据
export interface UserPasswordData {
  oldPassword?: string;
  newPassword?: string;
  confirmPassword?: string;
}

// 查询参数
export interface UserParams extends Partial<UserState> {
  current?: number;
  size?: number;
  deptId?: number;
  status?: number;
  startDate?: string;
  endDate?: string;
}

// 登录
export function login(params: LoginData) {
  return request<LoginRes>('get', `/oauth/token`, { params });
}

// 登录
export function logout() {
  return request<LoginRes>('post', '/logout');
}

// 获取登录用户详情
export function getUserInfo() {
  return request<UserState>('get', '/sys-user');
}

// 获取登录用户资源
export function getMenuList() {
  return request<ServerMenu[]>('get', '/resource/owner');
}

// 用户列表
export function pageSysUser(params: UserParams) {
  return request<Pagination<UserData[]>>('get', '/sys-user/page', { params })
}

// 用户信息
export function getSysUser(id: any) {
  return request<UserData>('get', '/sys-user/' + id)
}

// 新增用户
export function saveSysUser(data: UserData) {
  return request('post', '/sys-user', data)
}

// 修改用户
export function updateSysUser(data: UserData) {
  return request('put', '/sys-user', data)
}

// 修改用户状态
export function updateSysUserStatus(data: UserData) {
  return request('put', '/sys-user/status', data)
}

// 删除用户
export function deleteSysUser(ids: any) {
  return request('delete', '/sys-user/' + ids)
}

// 修改我的密码
export function updateSysUserPwd(data: UserPasswordData) {
  return request('put', '/sys-user/password/update', data)
}

// 重置用户密码
export function resetSysUserPwd(data: UserData) {
  return request('put', '/sys-user/password/reset', data)
}
